$(document).ready(function(){
    //商品照片上传
    function setGoodsImg(res){
        console.log(res);
        document.getElementById('goodsImgUrl').value = res
    }
    imgUpload('goodsImgInput','goodsImgShow',setGoodsImg);
    //获取分类
    var classList = [];
    let global_class_id = '';
    let goodsList = [];
    function getClass(){
        getAjax(`${global.apiUrl}/get_community_service_shop_product_class`,res=>{
            console.log(res);
            if(res){
                classList = res;
                let html = '';
                let add_html = `<option value="">请选择类别</option>`;
                for(var i in res){
                    html += `
                        <option value="${res[i].id}">${res[i].class_name}</option>
                    `
                    add_html += `
                        <option value="${res[i].id}">${res[i].class_name}</option>
                    `
                }
                $('#goods_class').html(html);
                $('#g_class').html(add_html);
                global_class_id = classList[0].id;
                getGoods(global_class_id);

            }
        },{
            shop_id:global.community_service_shop_id
        })
    }
    getClass();
    //获取分类商品
    function getGoods(class_id){
        getAjax(`${global.apiUrl}/get_community_service_shop_product`,res=>{
            console.log(res);
            var html = '';
            if(res.length != 0){
                goodsList = res;
                for(var i in res){
                    html += `
                     <tr>
                        <td class="img_style">
                            <img src="${res[i].head_img}" alt="">
                        </td>
                        <td>${res[i].product_name}</td>
                        <td>${res[i].price}</td>
                        <td>${res[i].all_pay}</td>
                        <td>
                            <button class="btn btn-primary" data-toggle="modal" data-target="#goodsModal" data-type="2" data-id="${res[i].id}">修改</button>
                            <button class="btn btn-success delete_btn" data-id="${res[i].id}" >删除</button>
                        </td>
                    </tr>
                `
                }
            }else{
              html = `
                <tr >
                    <td style="color:#aaa;text-align:center;" colspan="5">暂无商品</td>
                </tr>`
            }
            $('#product_list').html(html);
        },{
            shop_id: global.community_service_shop_id,
            class_id:class_id
        })
    }
    //切换类别
    $('#goods_class').on('change',function(){
       let class_id = $(this).children('option:selected').val();
       global_class_id = class_id;
        getGoods(class_id);
    })
    //添加商品，切换类别
    let select_class_id = '';
    $('#g_class').on('change',function(){
        let class_id = $(this).children('option:selected').val();
        $('#class_id').val(class_id);
    })
    /*
    * 添加商品块
    **/
    //modal  init
    $('#goodsModal').on('show.bs.modal',function(e){
        let modal = $(this);
        let btnThis = $(e.relatedTarget);
        let type = btnThis.data('type');//1新增  2修改
        console.log(type);
        modal.find('#set_type').val(type);
        modal.find('#g_name').val('');
        modal.find('#g_price').val('');
        modal.find('#g_old_price').val('');
        modal.find('#g_kuchun').val('');
        modal.find('#g_gg').val('');
        modal.find('#goodsImgUrl').val('');
        modal.find('#g_class option:first').prop('selected','selected');
        let img_show_html = `<div>图片预览</div>`;
        modal.find('#goodsImgShow').html(img_show_html);
        let modal_title = '';
        if(type==1){
            //新增
            modal_title = '添加商品';
        }
        if(type==2){
            //修改
            modal_title = '修改商品';
            let goods_id = btnThis.data('id');
            let goods_content = {};
            for(let i in goodsList){
                if(goodsList[i].id == goods_id){
                    goods_content = goodsList[i];
                }
            }
            modal.find('#goods_id').val(goods_id);
            modal.find('#g_name').val(goods_content.product_name);
            modal.find('#g_price').val(goods_content.price);
            modal.find('#g_old_price').val(goods_content.no_price);
            modal.find('#g_kuchun').val(goods_content.no_pay);
            modal.find('#g_gg').val(goods_content.gg);
            modal.find('#goodsImgUrl').val(goods_content.head_img);
            let img_show_html = `<img src="${goods_content.head_img}" />`;
            modal.find('#goodsImgShow').html(img_show_html);
            let index = '';
            for(let j in classList){
                if(goods_content.class_id == classList[j].id){
                    index = Number(j) + 1;
                }
            }
            modal.find('#class_id').val(goods_content.class_id);
            modal.find(`#g_class option:nth(${index})`).prop('selected','selected');
        }
        modal.find('#goodsModalLabel').text(modal_title);
    })
    //submit
    $('#submit_goods_btn').on('click',function(){
        let product_name = $('#g_name').val();
        let price = $('#g_price').val();
        let no_price = $('#g_old_price').val();
        let no_pay = $('#g_kuchun').val();
        let gg = $('#g_gg').val();
        let head_img = $('#goodsImgUrl').val();
        let type = $('#set_type').val();
        let pro = {};
        let class_id = $('#class_id').val();
        if(type == 1){
            pro = {
                product_name:product_name,
                price:price,
                no_price:no_price,
                no_pay:no_pay,
                gg:gg,
                head_img:head_img,
                class_id:class_id,
                shop_id:global.community_service_shop_id
            }
        }
        if(type == 2){
            let id = $('#goods_id').val();
            pro = {
                id:id,
                product_name:product_name,
                price:price,
                no_price:no_price,
                no_pay:no_pay,
                gg:gg,
                head_img:head_img,
                class_id:class_id,
                shop_id:global.community_service_shop_id
            }
        }
        console.log(pro);
        pro = JSON.stringify(pro);
        getAjax(`${global.apiUrl}/admin_operation_community_service_shop_product`,res=>{
            console.log(res);
            if(res == 1){
                alert('商品信息提交成功！');
                $('#goodsModal').modal('hide');
                getGoods(global_class_id);
            }
        },{
            type:type,
            pro:pro
        })
    })
    //delete
    $('#product_list').on('click','.delete_btn',function(){
        let id = $(this).data('id');
        console.log(id);
        let confirmIs = confirm('是否确实删除该商品？');
        if(confirmIs){
            getAjax(`${global.apiUrl}/admin_operation_community_service_shop_product`,res=>{
                if(res == 1){
                    alert('商品已删除！');
                    getGoods(global_class_id);
                }
            },{
                type:3,
                id:id
            })
        }
    })
})
